//
//  RoomMenuAninator.m
//  YSSmartHome
//
//  Created by yanghao on 2016/10/9.
//  Copyright © 2016年 yahaong. All rights reserved.
//

#import "RoomMenuAninator.h"
#import "RoomMenuPresentationController.h"

@interface RoomMenuAninator ()

@property(nonatomic, assign)BOOL isPresent;
@end
@implementation RoomMenuAninator
#pragma mark - UIViewControllerTransitioningDelegate

//谁来负责转场动画
- (nullable UIPresentationController *)presentationControllerForPresentedViewController:(UIViewController *)presented presentingViewController:(nullable UIViewController *)presenting sourceViewController:(UIViewController *)source NS_AVAILABLE_IOS(8_0) {
    
   RoomMenuPresentationController *roomMenuPresentVc=   [[RoomMenuPresentationController alloc]initWithPresentedViewController:presented presentingViewController:presenting];
    roomMenuPresentVc.presentFrame = self.presentFrame;
    return roomMenuPresentVc;
    
}
- (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source
{
    self.isPresent  = YES;
    return self;
}

- (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed{
    
    self.isPresent = NO;
    return self;
}

#pragma mark - UIViewControllerAnimatedTransitioning

//动画时长
- (NSTimeInterval)transitionDuration:(nullable id <UIViewControllerContextTransitioning>)transitionContext{
    return 1.0;
}
//如何动画
- (void)animateTransition:(id <UIViewControllerContextTransitioning>)transitionContext
{
    if (self.isPresent) {
        UIView *toView = [transitionContext viewForKey: UITransitionContextToViewKey];
        toView.transform = CGAffineTransformMakeScale(1.0, 0.0);
        [transitionContext.containerView addSubview:toView];
        toView.layer.anchorPoint = CGPointMake(0.5, 0);
        [UIView animateWithDuration:0.5 animations:^{
            toView.transform = CGAffineTransformIdentity;
        } completion:^(BOOL finished) {
            [transitionContext completeTransition:YES];
        }];
    } else {
        UIView *fromView = [transitionContext viewForKey: UITransitionContextFromViewKey];
        [UIView animateWithDuration:0.2 animations:^{
            fromView.transform = CGAffineTransformMakeScale(1.0, 0.000001);
        } completion:^(BOOL finished) {
            [transitionContext completeTransition:YES];
        }];
        
    }
}

@end
